home *** CD-ROM | disk | FTP | other *** search
/ Champak 141 / (Vol 141) Oct 17 2011.iso / Games / global-one-water.swf / scripts / __Packages / Kid.as < prev    next >
Encoding:
Text File  |  2011-10-17  |  10.1 KB  |  354 lines

  1. class Kid
  2. {
  3.    var engine;
  4.    var mc;
  5.    var skin;
  6.    var hair;
  7.    var body;
  8.    var feet;
  9.    var maxStamina;
  10.    var stamina;
  11.    var distAlongPath;
  12.    var following;
  13.    var endOfPathHandler;
  14.    var followPathUntil;
  15.    var pump;
  16.    var slotNum;
  17.    var oldPumpPos;
  18.    var n;
  19.    var leaving = false;
  20.    var _happy = true;
  21.    var _knackered = false;
  22.    static var KID_MC = "kid";
  23.    static var MIN_STAMINA = 1000;
  24.    static var MAX_STAMINA = 2000;
  25.    static var TIRED_POINT = 0.5;
  26.    static var EXHAUSTED_POINT = 0.25;
  27.    static var MAX_SLOW = -0.5;
  28.    static var RUN_TO_SLOT_SPEED = 0.015;
  29.    function Kid(e)
  30.    {
  31.       super();
  32.       this.engine = e;
  33.       this.mc = this.engine.back.attachMovie(Kid.KID_MC,Kid.KID_MC + getTimer(),0,{kid:this,isDownHill:false,engine:this.engine});
  34.       this.skin = Rand.random(1,this.mc.inner.head._totalframes,0);
  35.       this.hair = Rand.random(1,this.mc.inner.hair._totalframes,0);
  36.       this.body = Rand.random(1,this.mc.inner.body._totalframes,0);
  37.       this.feet = Rand.random(1,this.mc.inner.leftFoot._totalframes,0);
  38.       this.maxStamina = this.stamina = Rand.random(Kid.MIN_STAMINA,Kid.MAX_STAMINA,0);
  39.       this.setAvatar();
  40.       this.followPath(this.engine.enteringLabourPool1,this.swapMcToFront);
  41.    }
  42.    function setAvatar()
  43.    {
  44.       this.mc.inner.head.gotoAndStop(this.skin);
  45.       this.mc.inner.hair.gotoAndStop(this.hair);
  46.       this.mc.inner.body.gotoAndStop(this.body);
  47.       this.mc.inner.leftHand.gotoAndStop(this.skin);
  48.       this.mc.inner.rightHand.gotoAndStop(this.skin);
  49.       this.mc.inner.leftFoot.gotoAndStop(this.feet);
  50.       this.mc.inner.rightFoot.gotoAndStop(this.feet);
  51.       var _loc2_ = 1 - this.stamina / this.maxStamina;
  52.       this.mc.inner.head.red.gotoAndStop(Math.floor(_loc2_ * (this.mc.inner.head.red._totalframes - 1)) + 1);
  53.    }
  54.    function followPath(path, eop, until, startPoint)
  55.    {
  56.       this.distAlongPath = !isNaN(startPoint) ? startPoint : 1;
  57.       this.following = path;
  58.       this.endOfPathHandler = eop;
  59.       this.followPathUntil = !isNaN(until) ? until : path.length;
  60.    }
  61.    function update(f)
  62.    {
  63.       if(this.following)
  64.       {
  65.          this.mc.setPos(this.following.getPositionAtFrame(++this.distAlongPath,this.mc._parent));
  66.          if(this.distAlongPath >= this.followPathUntil)
  67.          {
  68.             this.reachedEndOfPath();
  69.          }
  70.       }
  71.       else if(this.pump)
  72.       {
  73.          if(this.slotNum !== undefined)
  74.          {
  75.             var _loc4_ = this.pump.getPositionOfSlot(this.slotNum);
  76.             var _loc2_ = undefined;
  77.             var _loc3_ = undefined;
  78.             if(_loc4_ > this.distAlongPath)
  79.             {
  80.                _loc2_ = _loc4_ - this.distAlongPath;
  81.                if(_loc2_ > 0.5)
  82.                {
  83.                   _loc2_ = 1 - _loc2_;
  84.                   _loc3_ = -1;
  85.                }
  86.                else
  87.                {
  88.                   _loc3_ = 1;
  89.                }
  90.             }
  91.             else
  92.             {
  93.                _loc2_ = this.distAlongPath - _loc4_;
  94.                if(_loc2_ > 0.5)
  95.                {
  96.                   _loc2_ = 1 - _loc2_;
  97.                   _loc3_ = 1;
  98.                }
  99.                else
  100.                {
  101.                   _loc3_ = -1;
  102.                }
  103.             }
  104.             if(_loc2_ <= Kid.RUN_TO_SLOT_SPEED)
  105.             {
  106.                this.reachedSlot();
  107.             }
  108.             else
  109.             {
  110.                if(_loc3_ > 0)
  111.                {
  112.                   this.distAlongPath += Kid.RUN_TO_SLOT_SPEED;
  113.                }
  114.                else
  115.                {
  116.                   this.distAlongPath -= Kid.RUN_TO_SLOT_SPEED;
  117.                }
  118.                this.distAlongPath = this.numberWrap(this.distAlongPath,1);
  119.                var _loc5_ = Math.floor(this.distAlongPath * this.pump.goToSlotGuide.length) + 1;
  120.                this.mc.setPos(this.pump.goToSlotGuide.getPositionAtFrame(_loc5_,this.mc._parent));
  121.             }
  122.          }
  123.          else
  124.          {
  125.             this.stamina -= this.pump.effort;
  126.             var _loc6_ = this.pump.getPositionOfSlot(this.pump.getSlotForKid(this));
  127.             if(this.stamina < 0)
  128.             {
  129.                this.stamina = 0;
  130.             }
  131.             this.oldPumpPos = _loc6_;
  132.          }
  133.       }
  134.       if(!this.leaving)
  135.       {
  136.          if(this.happy)
  137.          {
  138.             this.checkIfHappy();
  139.             if(!this.happy)
  140.             {
  141.                this.mc.startSweating();
  142.                this.engine.tiredTip();
  143.             }
  144.          }
  145.          if(!this.happy)
  146.          {
  147.             if(!this.knackered)
  148.             {
  149.                this.checkIfKnackered();
  150.                if(this.knackered)
  151.                {
  152.                   this.mc.changeKnackered(true);
  153.                   this.engine.exhaustedTip();
  154.                }
  155.             }
  156.          }
  157.       }
  158.       this.mc.update();
  159.       this.mc.inner.head.setTiredness(1 - this.stamina / this.maxStamina);
  160.    }
  161.    function numberWrap(n, r)
  162.    {
  163.       n %= r;
  164.       return n >= 0 ? n : r + n;
  165.    }
  166.    function joinPump(p, sn)
  167.    {
  168.       this.pump = p;
  169.       this.slotNum = sn;
  170.       this.distAlongPath = 0;
  171.    }
  172.    function reachedSlot()
  173.    {
  174.       this.oldPumpPos = this.pump.getPositionOfSlot(this.slotNum);
  175.       this.mc.joinedPump();
  176.       this.pump.slots[this.slotNum] = this;
  177.       delete this.slotNum;
  178.    }
  179.    function leavePump()
  180.    {
  181.       var _loc2_ = this.pump.getPositionOfSlot(this.pump.getSlotForKid(this));
  182.       this.pump.leave(this);
  183.       var _loc3_ = undefined;
  184.       if(_loc2_ > PlayPump.KID_LEAVE_POINT)
  185.       {
  186.          _loc3_ = this.pump.leaveRight;
  187.          _loc2_ = 1 - _loc2_;
  188.       }
  189.       else
  190.       {
  191.          _loc3_ = this.pump.leaveLeft;
  192.       }
  193.       _loc2_ *= 2;
  194.       this.followPath(_loc3_,this.joinPumpExit,_loc3_.length,Math.floor(_loc3_.length * _loc2_) + 1);
  195.       this.mc.leftPump();
  196.       this.mc.stopSweating();
  197.       this._knackered = false;
  198.       this.mc.changeKnackered(false);
  199.       this.leaving = true;
  200.    }
  201.    function joinPumpExit()
  202.    {
  203.       this.followPath(this.pump.exit,Delegate.create(this,this.followPath,this.engine.leavingGame1,Delegate.create(this,this.swapMcToBack)));
  204.       delete this.pump;
  205.    }
  206.    function reachedEndOfPath()
  207.    {
  208.       delete this.following;
  209.       delete this.followPathUntil;
  210.       var _loc2_ = this.endOfPathHandler;
  211.       delete this.endOfPathHandler;
  212.       _loc2_.call(this);
  213.    }
  214.    function goTo(queue)
  215.    {
  216.       this.followPath(queue.path,Delegate.create(this,this.isKidAtEndOfQueue,queue),1);
  217.       queue.joinedQueue(this);
  218.    }
  219.    function joinedQueue(queue)
  220.    {
  221.       this.isKidAtEndOfQueue(queue);
  222.    }
  223.    function sendToNewPath(newPath, followedPath)
  224.    {
  225.       if(this.following === undefined)
  226.       {
  227.          this.followPath(newPath,followedPath);
  228.       }
  229.       else
  230.       {
  231.          this.followPath(this.following,Delegate.create(this,this.followPath,newPath,followedPath),null,this.distAlongPath);
  232.       }
  233.    }
  234.    function sendToNewQueue(newQueue, fromQueue)
  235.    {
  236.       if(this.following === undefined)
  237.       {
  238.          this.goTo(newQueue);
  239.       }
  240.       else
  241.       {
  242.          this.followPath(this.following,Delegate.create(this,this.goTo,newQueue),null,this.distAlongPath);
  243.       }
  244.       fromQueue.kidLeaveQueue();
  245.    }
  246.    function isKidAtEndOfQueue(queue)
  247.    {
  248.       var _loc2_ = 0;
  249.       while(_loc2_ < queue.length)
  250.       {
  251.          if(queue[_loc2_] == this)
  252.          {
  253.             var _loc4_ = queue.path.length - Queue.QUEUE_SEPARATION * _loc2_;
  254.          }
  255.          _loc2_ = _loc2_ + 1;
  256.       }
  257.       if(this.distAlongPath != _loc4_)
  258.       {
  259.          this.followPath(queue.path,Delegate.create(this,this.isKidAtEndOfQueue,queue),this.distAlongPath + 1,this.distAlongPath);
  260.       }
  261.       else
  262.       {
  263.          this.mc.setPose(KidMc.IDLE);
  264.       }
  265.    }
  266.    function swapMcToFront()
  267.    {
  268.       var _loc2_ = this.mc._name;
  269.       var _loc3_ = this.mc.getTargetDepth();
  270.       this.mc.die();
  271.       this.mc = this.engine.pumpsHolder.attachMovie(Kid.KID_MC,_loc2_,_loc3_,{kid:this,isDownHill:true,engine:this.engine,xdir:-1});
  272.       this.setAvatar();
  273.       this.mc.setPos(this.engine.enteringLabourPool1.getPositionAtFrame(this.engine.enteringLabourPool1.length - 1,this.mc._parent));
  274.       this.goTo(this.engine.labourPool);
  275.       this.update();
  276.    }
  277.    function swapMcToBack()
  278.    {
  279.       var _loc2_ = this.mc._name;
  280.       var _loc3_ = this.mc.getTargetDepth();
  281.       this.mc.die();
  282.       this.mc = this.engine.back.attachMovie(Kid.KID_MC,_loc2_,_loc3_,{kid:this,isDownHill:false,engine:this.engine});
  283.       this.setAvatar();
  284.       this.followPath(this.engine.leavingGame2,Delegate.create(this,this.die));
  285.       this.update();
  286.    }
  287.    function checkIfHappy()
  288.    {
  289.       if(this.stamina / this.maxStamina < Kid.TIRED_POINT)
  290.       {
  291.          this._happy = false;
  292.       }
  293.       else
  294.       {
  295.          this._happy = true;
  296.       }
  297.    }
  298.    function checkIfKnackered()
  299.    {
  300.       if(this.stamina / this.maxStamina < Kid.EXHAUSTED_POINT)
  301.       {
  302.          this._knackered = true;
  303.       }
  304.       else
  305.       {
  306.          this._knackered = false;
  307.       }
  308.    }
  309.    function die()
  310.    {
  311.       if(this.mc)
  312.       {
  313.          this.mc.die();
  314.       }
  315.       var _loc2_ = 0;
  316.       while(_loc2_ < this.engine.allKids.length)
  317.       {
  318.          if(this.engine.allKids[_loc2_] == this)
  319.          {
  320.             this.engine.allKids.splice(_loc2_,1);
  321.             break;
  322.          }
  323.          _loc2_ = _loc2_ + 1;
  324.       }
  325.       this.engine.indQueue.push(this.n);
  326.    }
  327.    function get happy()
  328.    {
  329.       return this._happy;
  330.    }
  331.    function get knackered()
  332.    {
  333.       return this._knackered;
  334.    }
  335.    function get pushForce()
  336.    {
  337.       var _loc2_ = this.stamina / this.maxStamina;
  338.       if(_loc2_ >= Kid.TIRED_POINT)
  339.       {
  340.          return 1;
  341.       }
  342.       if(_loc2_ < Kid.EXHAUSTED_POINT)
  343.       {
  344.          return (Kid.EXHAUSTED_POINT - _loc2_) / Kid.EXHAUSTED_POINT * Kid.MAX_SLOW;
  345.       }
  346.       _loc2_ -= Kid.EXHAUSTED_POINT;
  347.       return _loc2_ / (Kid.TIRED_POINT - Kid.EXHAUSTED_POINT);
  348.    }
  349.    function toString()
  350.    {
  351.       return "kid " + this.n;
  352.    }
  353. }
  354.